package email;

import jdbc.JdbcDao;

/**
 * 这是一个方法类，用来在忘记密码的时候找回密码
 * @author bo
 *
 */
public class ForgetPassword {
	public static boolean forgetPassword(String email) {
		try {
			JdbcDao mysql = new JdbcDao();
			if (mysql.getPersonalInfo(email, "email").equals(email)){
				String newPassword = GetValidCode.getValidCode();
				mysql.changePassword(email, String.valueOf(newPassword.hashCode()));
				
				MailSenderInfo mailInfo = new MailSenderInfo();
				mailInfo.setMailServerHost("smtp.qq.com");      
			    mailInfo.setMailServerPort("25");      
			    mailInfo.setValidate(true);      
			    mailInfo.setUserName("2659648673@qq.com");      
			    mailInfo.setPassword("njl9123");
			    mailInfo.setFromAddress("2659648673@qq.com");      
			    mailInfo.setToAddress(email);
			    mailInfo.setSubject("新版信息门户密码找回");      
			    mailInfo.setContent("请点击以下链接找回您的密码：  http://localhost:8080/Info/forgetPassword.jsp?newpassword=" + newPassword); 
			    SendEmail sm = new SendEmail();
			    sm.sendTextMail(mailInfo);
			    System.out.println("密码找回邮件已发送");
			    return true;
			}
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
